﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class VSController : MonoBehaviour
{
    // Start is called before the first frame update
    private static VSController m_Instance = null;
    public static VSController Instance {
        get {
            if(m_Instance == null) {
                m_Instance = GameObject.FindObjectOfType<VSController>() as VSController;
            }
            if(!m_Instance) {
                m_Instance = (new GameObject("VSController")).AddComponent<VSController>();
            }
            return m_Instance;
        }
    }
    [SerializeField]
    private Side[] PlayerSide = new Side[2];
    private int CurrSideIndex;
    public List<CardProps.ID> CommonDeck = new List<CardProps.ID>();
    public BoardPlacer[] boardPlacers = new BoardPlacer[24];
    private int m_RoundSettleCount = 0;
    
    //初始化对局
    void Start() {
        InitVS();
    }
    void InitVS() {
        PlayerSide[0].Init();
        PlayerSide[1].Init();
        CurrSideIndex = 0;
    }
    public Side GetCurrSide() {
        return PlayerSide[CurrSideIndex];
    }
    public Side GetOppositeSide() {
        if(CurrSideIndex == 0) return PlayerSide[1];
        else return PlayerSide[0];
    }
    public void Settle(CardUI cardUI, BoardPlacer boardPlacer) {
        Side side = GetCurrSide();

        Card settledCard = side.SettleCard(cardUI.HandIndex, boardPlacer.BoardType, boardPlacer.Index);
        side.Coin -= m_RoundSettleCount * 5 + side.Hand[cardUI.HandIndex].Cost;
        m_RoundSettleCount += 1;
        boardPlacer.RefreshState();

        //效果发动
        settledCard.GetComponent<IEffectSettler>().OnSettle(); 

        VSUI.Instance.DestroyCard(cardUI.HandIndex);
    }
    public bool CheckCanSettle(int handIndex) {
        Side side = GetCurrSide();
        Debug.Log(side.Hand[handIndex].Cost + "Cost");
        if(side.Hand[handIndex].Cost + m_RoundSettleCount * 5 > side.Coin) 
            return false;
        return true;
    }
    
    //抽卡阶段
    void Draw() {
        GetCurrSide().DrawCard();
    }
    void RoundStart() {
        Side side = GetCurrSide();
        foreach(Card card in side.ExposedField) {
            if(card != null) {
                card.GetComponent<IEffectSettler>().OnRoundStart();
            }
        }
        foreach(Card card in side.HiddenField) {
            if(card != null) {
                card.GetComponent<IEffectSettler>().OnRoundStart();
            }
        }
    }
    //游戏结束
    void GameOver() {

    }

    //换边
    void SwitchSide() {
        m_RoundSettleCount = 0;
        GetCurrSide().Coin *= 1.1f;
        
        CurrSideIndex = (CurrSideIndex == 0 ? 1 : 0);
        foreach(BoardPlacer boardPlacer in boardPlacers) {
            boardPlacer.RefreshState();
        }
        GetCurrSide().RefreshCard();
        
        GetCurrSide().Coin += 3;
    }
    public void RefreshBoard() {
        foreach(BoardPlacer boardPlacer in boardPlacers) {
            boardPlacer.RefreshState();
        }
    }

    void RoundEnd() {
        
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.F)) {
            Draw();
        }
        if(Input.GetKeyDown(KeyCode.R)) {
            SwitchSide();
        }
    }
}
